﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xabe.FFmpeg;
using System.Linq;

namespace CBCore
{
    public class FFmpegVideoInfo
    {
        public int Height { get; set; }
        public int Width { get; set; }
        public double Seconds { get; set; }
    }
    class VideoHelper
    {
        public static async Task Cut(string sourceVideoFullPath, string outVideoFullPath, int startSeconds, int cutSeconds)
        {
            var mi = await FFmpeg.Conversions.FromSnippet.Split(sourceVideoFullPath, outVideoFullPath, TimeSpan.FromSeconds(startSeconds), TimeSpan.FromSeconds(cutSeconds));
            await mi.Start();
        }
        public static async Task Merge(string outFullPath,string[] videos)
        {
            var mi = await FFmpeg.Conversions.FromSnippet.Concatenate(outFullPath,videos);
            await mi.Start();
        }
        public static async Task<FFmpegVideoInfo> VideoInfoAsync(string path)
        {
            var info = new FFmpegVideoInfo();

            IMediaInfo mediaInfo = await FFmpeg.GetMediaInfo(path);

            var vinfo = mediaInfo.VideoStreams.FirstOrDefault();
            if (vinfo == null)
            {
                return null;
            }
            info.Height = vinfo.Height;
            info.Width = vinfo.Width;
            info.Seconds = vinfo.Duration.TotalSeconds;

            return info;
        }
    }
}
